<template>
  <div>
    <el-table :data="tableData" style="width: 100%;margin-bottom: 20px;" row-key="id" border>
      <el-table-column prop="id" label="角色编号" sortable width="180"></el-table-column>
      <el-table-column prop="rolename" label="角色名称" width="180"></el-table-column>
      <el-table-column label="状态" width="180">
        <template v-slot="scope">
          <el-button v-if="scope.row.status === 1" type="success">启用</el-button>
          <el-button v-else type="danger">禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" sortable width="180">
        <template v-slot="scope">
          <el-button type="success" @click="edit(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { roleList ,delRole} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  data() {
    return {
      tableData: []
    };
  },
  methods: {
    requestList() {
      roleList().then(res => {
        if (res.code === 200) {
          this.tableData = res.list;
        }
      });
    },
    edit(id){
        this.$emit('edit',id)
    },
    del(id){
        this.$confirm('确定要删除吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
                }).then(() => {
                    // 发起删除请求
                    delRole({id}).then(res=>{
                        if(res.code === 200){
                            // 消息提示
                            successAlert(res.msg)
                            // 刷新列表数据
                            this.requestList()
                        }
                    })
                }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
                });
    }
  },
  mounted() {
    this.requestList();
    this.$eventBus.$on('message',()=>{
            this.requestList()
        })
  }
};
</script>

<style>
</style>
